/*!
 * UEditor
 * version: ueditor
 * build: Wed Aug 10 2016 11:06:16 GMT+0800 (CST)
 */
/* 添加图片按钮*/
	UE.registerUI('button', function(editor, uiName) {
    editor.registerCommand(uiName, {
        execCommand: function() {
        	alert(uiName);
        }
    });
    var btn = new UE.ui.Button({
        name: "button",
        cssRules: 'background-position: -380px 0px;',
        title: "添加图片",
        onclick: function() {
        	$("#addImg").css("display","block");
        	$("#addImgSpan").css("display","inline-block");
			$("#submit").css("display","none");
			$("#again").css("display","none");
			$("#minImgdiv").css("display","none");
			$("#file").css("display","inline");
			$("#imgspan").css("display","block");
        }
    });
    editor.addListener('selectionchange', function() {
        var state = editor.queryCommandState(uiName);
        if (state == -1) {
            btn.setDisabled(true);
            btn.setChecked(false);
        } else {
            btn.setDisabled(false);
            btn.setChecked(state);
        }
    });
    
    return btn;
});
	/* 预览按钮*/
	UE.registerUI('button2', function(editor, uiName) {
	    editor.registerCommand(uiName, {
	        execCommand: function() {
	        	alert(uiName);
	        }
	    });
	    var btn2 = new UE.ui.Button({
	        name: "button2",
	        cssRules: 'background-position: -420px -20px;',
	        title: "预览",
	        onclick: function() {
	        	$("#maindiv").css("display","none");
	        	$("#preview").css("display","block")
	        	$("#outPreview").css("display","inline")
	        	$("#preview").html(getContent());
	        }
	    });
	    editor.addListener('selectionchange', function() {
	        var state = editor.queryCommandState(uiName);
	        if (state == -1) {
	            btn.setDisabled(true);
	            btn.setChecked(false);
	        } else {
	            btn.setDisabled(false);
	            btn.setChecked(state);
	        }
	    });
	    
	    return btn2;
	});
	
$(document).ready(function(e){
	var allpath=$("#path").val();
	setTimeout(function () {
		var html=$("#preview").html();
		insertHtml(html);
    }, 1000);
	var img0,img1,img2,img3;
		var imgpath;
		var list;
	$("#upload").change(function(e) {
		var imgs=document.getElementById("upload").files;
		if(imgs.length>4){
			alert("一次不能上传超过4张图片");
			return false;
		}
		// 创建表单对象 
		var formData = new FormData();
			for(var i=0;i<imgs.length;i++){
//				 获取文件名,判断是否是图片 
				if (!/.(gif|jpg|jpeg|png|GIF|JPG|png)$/.test(imgs[i].name)) {
					alert("图片类型必须是.gif,jpeg,jpg,png中的一种");
					return false;
				}
				 //添加文件和id 
				formData.append("img"+i, imgs[i]);
			}
		$.ajax({
			url :allpath+"commodity/uploadImgs.action",
			type : 'POST',
			data : formData,
			processData : false,
			contentType : false,
			dataType : "json",
			success : function(data) {
				img0=data.img0;
				img1=data.img1;
				img2=data.img2;
				img3=data.img3;
				list=[img0,img1,img2,img3];
					if(list[0]!=null){
						$("#img0").attr("src",allpath+"file/detailed/"+list[0]);
						$("#closImg0").css("display","inline");
					}
					if(list[1]!=null){
						$("#img1").attr("src",allpath+"file/detailed/"+list[1]);	
						$("#closImg1").css("display","inline");
					}
					if(list[2]!=null){
						$("#img2").attr("src",allpath+"file/detailed/"+list[2]);	
						$("#closImg2").css("display","inline");
					}
					if(list[3]!=null){
						$("#img3").attr("src",allpath+"file/detailed/"+list[3]);	
						$("#closImg3").css("display","inline");
					}
				$("#submitImg").css("display","inline");
				$("#file").css("display","none");
				$("#imgspan").css("display","none");
					
			},
			error : function(responseStr) { 
				console.log("error");
				} 
			});
});
	/* 确认插入图片按钮*/
	$("#submitImg").click(function(){
		if(list[0]!=null){
insertHtml("<img  src='"+allpath+"file/detailed/"+list[0]+"' /><br>");
	$("#img0").attr("src","img/imgDefault.png");
			img0=null;
			$(".defaultImgDiv>button").css("display","none");
		}
		if(list[1]!=null){
			insertHtml("<img  src='"+allpath+"file/detailed/"+list[1]+"' /><br>");
			$("#img1").attr("src",allpath+"img/imgDefault.png");
			img1=null;
			
					}
		if(list[2]!=null){
			insertHtml("<img  src='"+allpath+"file/detailed/"+list[2]+"' /><br>");
			$("#img2").attr("src",allpath+"img/imgDefault.png");
			img2=null;
					}
		if(list[3]!=null){
			insertHtml("<img  src='"+allpath+"file/detailed/"+list[3]+"' /><br>");
			$("#img3").attr("src",allpath+"img/imgDefault.png");
			img3=null;
					}
		
		$("#addImg").css("display","none");
		$("#submitImg").css("display","none");
	});	
	$("#release").click(function(){
				var title=$("#title").val();
		if(title==""||!hasContent()){
			$("#titleError").html("内容不能为空");
			return false;
		}else{
			var url=allpath+"result/bbsList!addTitle";
			var text=getContent();
			var val={"text":text,"title":title};
			$.post(url,val,function(r){
				if(r.result=="error"){
					$("#titleError").html("出现错误,请重试");
				}else{
					window.location.href=allpath+"main/floor?titleId="+r.result+"&floorCount=1&title="+title;
				}
				
			});
		}
		return false;
	});
		/* 退出预览按钮*/
$("#outPreview").click(function(){
	$("#preview").css("display","none");
	$("#maindiv").css("display","block");
	$("#outPreview").css("display","none")
	
});
//删除将要插入的图片
$("#closImg0").click(function(){
	var src=list[0];
	var val={'imgName':src};
	var url=allpath+"commodity/deleteImg.action"; 
	$.post(url,val,function(data){
		
	});
	$("#img0").attr("src",allpath+"img/imgDefault.png");
	$("#closImg0").css("display","none");
	list[0]=null;
	
});
$("#closImg1").click(
		function(){
			var src=list[1];
			var val={'imgName':src};
			var url=allpath+"commodity/deleteImg.action"; 
			$.post(url,val,function(data){
				
			});
			$("#img1").attr("src",allpath+"img/imgDefault.png");
			$("#closImg1").css("display","none");
			list[1]=null;
		}
);
$("#closImg2").click(function (){
	var src=list[2];
	var val={'imgName':src};
	var url=allpath+"commodity/deleteImg.action"; 
	$.post(url,val,function(data){
		
	});
	$("#img2").attr("src",allpath+"img/imgDefault.png");
	$("#closImg2").css("display","none");
	list[2]=null;
});
$("#closImg3").click(function(){
	var src=list[3];
	var val={'imgName':src};
	var url=allpath+"commodity/deleteImg.action"; 
	$.post(url,val,function(data){
		
	});
	$("#img3").attr("src",allpath+"img/imgDefault.png");
	$("#closImg3").css("display","none");
	list[3]=null;
});
$("#closImg").click(function() {
	$("#addImg").css("display", "none");
	for(var i=0;i<4;i++){
		if(list[i]!=null){
			var src=list[i];
			var val={'imgName':src};
			var url=allpath+"commodity/deleteImg.action"; 
			$.post(url,val,function(data){
				
			});
			$("#img"+i).attr("src",allpath+"img/imgDefault.png");
			$("#closImg"+i).css("display","none");
			list[i]=null;
		}
	}
	$("#submitImg").css("display","none");
});
});
